package group2assignone.ucm.customer;

import group2assignone.dao.MyDAO;
import group2assignone.func.Authentication;
import group2assignone.model.Customer;
import group2assignone.ucm.UCController;
import group2assignone.ucm.UCStartup;
import group2assignone.view.UITool;
import group2assignone.view.customer.EditCustomerView;
import group2assignone.view.customer.CustomerWelcomeView;
import javax.swing.JOptionPane;

/**
 * Use Case: Edit Customer Account
 * Pre-Condition: User inputs details and selects 'Edit Account' 
 * from the edit account view
 * Post-Condition: UI is updated
 * 1. System prompts user for confirmation to edit the account
 * 2. System uploads the new customer details from the view
 * 3. System checks the fields were inputted correctly
 * 4. System edits the account
 * 5. System notifies the user the account has been created (with a message)
 * 6. System updates UI to the customer welcome view, with the new customer details
 * 3a. - System cannot verify details (incorrect data input)
 *     - System displays error message and prompts user to input correct data
 * 4a. - System cannot edit account (connection error)
 *     - System displays error message which prompts user to try again later
 * @author group2
 */
public class UCEditCustomerAccount implements UCController {

    private Customer customer;
    private Customer newCust;
    private boolean authOk,  okDAO;
    private Authentication auth;
    private EditCustomerView view;
    private int option;
    private MyDAO coms;
    private CustomerWelcomeView view2;

    /**
     * sets the model(customer) and view
     * @param customer
     * @param view
     */
    public UCEditCustomerAccount(Customer customer, EditCustomerView view) {
        this.customer = customer;
        this.view = view;
        newCust = new Customer();
        auth = new Authentication();
        coms = UCStartup.getInstance().getDAO();
    }

    /**
     *allows the user to edit their account once
     * they are logged in.
     */
    public void run() {
        //1. System prompts user for confirmation to edit the account
        option = view.confirmEdit();
        if (option == JOptionPane.YES_OPTION) {
            //2. System uploads the new customer details from the view
            view.upLoad(newCust);
            //3. System checks the fields were inputted correctly
            authOk = auth.checkCustFields(newCust);
            if (authOk) {
                //4. System edits the account
                okDAO = coms.sQLEditCustomer(newCust, customer.getUsername());
                if (okDAO) {
                    customer = newCust;
                    //5. System notifies the user the account has been created (with a message)
                    view.notifyEdit();
                    //6. System updates UI to the customer welcome view, with the new customer details
                    view2 = new CustomerWelcomeView(newCust);
                    view2.go();
                    view.dispose();
                } else {//4a. - System cannot edit account (connection error)
                    //System displays error message which prompts user to try again later
                    UITool.displayError("Cannot connect to database at this time. Please try again later.", view);
                }
            } else {//3a. - System cannot verify details (incorrect data input)
                //System displays error message and prompts user to input correct data
                UITool.displayError(auth.getErrorMsg(), view);
            }
        }
    }
}

